<template>
	<view>
		<view class="search-box">
			<!-- @confirm="search" -->
			<uni-search-bar @input="input" cancelButton="none" radius="100" :focus="true">
			</uni-search-bar>
		</view>
		<!-- 建议列表 -->
		<view class="sugg-list" v-if="searchResult.length!==0">
			<view class="sugg-item" v-for="(item,i) in searchResult" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrow-right" size="18"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<view>搜索历史</view>
				<uni-icons type="trash" size="20" @click="clean"></uni-icons>
			</view>
			<!-- 历史列表 -->
			<view class="history-list">
				<view v-for="(item,i) in histories" :key="i">
					
					<uni-tag :text="item" :inverted="true" @click="GotoGoodsList(item)"/>
				</view>
				
			</view>
		</view>



	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchResult: [],
				searchHistory: [],
			}
		},
		onLoad(){
			this.searchHistory=JSON.parse(uni.getStorageSync('kw')||'[]')
		},
		methods: {
			clean(){
				this.searchHistory=[],
				uni.setStorageSync('kw','[]')
			},
			GotoGoodsList(kw){
			uni.navigateTo({
				url:'/subpkg/goods_list/goods_list?query='+kw
			})	
			},
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getResearchList()
				}, 500)
			},
			async getResearchList() {
				if (this.kw === '') {
					this.searchResult = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResult = res.message
				this.saveSearchHistory()
			},
			gotoDetail(item) {
				console.log(item.goods_id)
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			saveSearchHistory(){
				const set=new Set(this.searchHistory)
				// this.searchHistory.push(this.kw)
				set.delete(this.kw)
				set.add(this.kw)
				this.searchHistory=Array.from(set)
				uni.setStorageSync('kw',JSON.stringify(this.searchHistory))
			}
		},
		computed:{
			histories(){
				return [...this.searchHistory].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		background-color: #f8789a;
		z-index: 999;
		top: 0;
		position: sticky;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			display: flex;
			// height: 55px;
			padding: 10px 0;
			font-size: 13px;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid lightgray;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 5px;
			}
		}
	}

	.history-box {
		padding: 0 5px;
		.history-title {
			display: flex;
			justify-content: space-between;
			height: 30px;
			font-size: 15px;
			align-items: center;
			border-bottom: 1px solid darkgrey;
		}
		.history-list {
			display:flex;
			flex-wrap: wrap;
			margin: 10px;
			uni-tag{
				margin-right: 10px;
			}		
		}
	}
</style>